package com.shg.web.action;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.shg.bean.PageView;
import com.shg.bean.Power;
import com.shg.bean.Roles;
import com.shg.service.PowerService;

@Controller
public class PowerAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Resource PowerService powerService;
	//分页权限
	private PageView<Power> pageView;
	//每页显示的最大记录数
	private int maxRecord=3;
	//但前页
	private int currentPage=1;
	//权限
	private Power power;
	//角色列表
	private List<Roles> roles;
	//权限
	private List<Power> powerList;
	//角色ID
	private long[] roleid;

	private String message;
	/**
	 * 所有的权限列表
	 * @return
	 */
	public String allPower(){
		pageView=new PageView<Power>(maxRecord, currentPage);
		try {
			pageView.setQueryResult(powerService.getAllPower(pageView.getFirstResult(), pageView.getMaxrecord()));
		} catch (Exception e) {
			e.printStackTrace();
			this.message="操作发生异常";
			return "error";
		}
		return "allPower";
	}
	public String editPower(){
		//将AJAX发送过来中文进行转码
		try {
			power.setName(URLDecoder.decode(power.getName(),"UTF-8"));
			power.setRemarks(URLDecoder.decode(power.getRemarks(),"UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		};
		try {
			powerService.updatePower(power);
		} catch (Exception e) {
			e.printStackTrace();
			this.message="操作发生异常";
			return "error";
		}
		return  allPower();
	}
	/**
	 * 静态模板查询
	 */
	public String html(){
		return "html";
	}
	/**
	 * 添加权限
	 * @return
	 */
	public String updateRole(){
		try {
			powerService.updatePowerForRole(power,roleid);
		} catch (Exception e) {
			e.printStackTrace();
			this.message="操作发生异常";
			return "error";
		}
		roleid=null;
		return SUCCESS;
	}
	/**
	 * 权限的锁定与解锁
	 * @return
	 */
	public String locked(){
		try {
			powerService.updateUnlock(power.getState(), power.getId());
		} catch (Exception e) {
			e.printStackTrace();
			this.message="操作发生异常";
			return "error";
		}
		return  allPower();
	}
	/**
	 * 查询权限关联的角色
	 * @return
	 */
	public String powerForRole(){
		try {
			roles = powerService.getPowerForRole(power.getId());
		} catch (Exception e) {
			e.printStackTrace();
			this.message="操作发生异常";
			return "error";
		}
		return "powerForRole";
	}
	/**
 	 * 更新角色的权限
 	 * @return
 	 * */
	public String updatePower(){
		return SUCCESS;
	}
	/**
 	 * 添加权限
 	 * @return
 	 */
 	public String addPowerUI(){
 		return "addPowerUI";
 	}
 	/**
 	 * 添加权限
 	 */
 	public String addPower(){
 		try {
			powerService.addPower(power);
		} catch (Exception e) {
			e.printStackTrace();
			this.message="添加权限产生异常";
			return "error";
		}
		this.message="成功添加了权限";
 		return "success";
 	}
 	/**
 	 * 获取权限的父类
 	 * @return
 	 */
 	public String powerParent(){
 		try {
 			powerList = powerService.getParent();
		} catch (Exception e) {
			this.message="获取权限父类产生异常";
			e.printStackTrace();
			return "error";
		}
 		return "powerParent";
 	}
	public PageView<Power> getPageView() {
		return pageView;
	}

	public void setPageView(PageView<Power> pageView) {
		this.pageView = pageView;
	}

	public int getMaxRecord() {
		return maxRecord;
	}

	public void setMaxRecord(int maxRecord) {
		this.maxRecord = maxRecord;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public Power getPower() {
		return power;
	}
	public void setPower(Power power) {
		this.power = power;
	}
	public List<Roles> getRoles() {
		return roles;
	}
	public void setRoles(List<Roles> roles) {
		this.roles = roles;
	}
	public long[] getRoleid() {
		return roleid;
	}
	public void setRoleid(long[] roleid) {
		this.roleid = roleid;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public List<Power> getPowerList() {
		return powerList;
	}
	public void setPowerList(List<Power> powerList) {
		this.powerList = powerList;
	}

}
